package repository

import (
	"gin-shop-admin/dao"
	"gin-shop-admin/models"
	"sort"
)

func GetReportData() (date_list []string, area_list []string, data_list [][]int, err error) {
	var reports []models.Report
	if err = dao.DB.Order("rp1_area desc, rp1_date").Find(&reports).Error;err!= nil{
		return
	}
	var date_map map[string]bool
	date_map = make(map[string]bool)
	var area_map map[string][]int
	area_map = make(map[string][]int)
	for _, report := range reports{
		date := report.Date.Format("2006-01-02")
		_, ok := date_map[date]
		if !ok {
			date_map[date] = true
		}
		area_map[report.Area] = append(area_map[report.Area], report.UserCount)
	}
	for date, _ := range date_map{
		date_list = append(date_list, date)
	}
	for k := range area_map{
		area_list = append(area_list, k)
	}
	sort.Strings(date_list)
	sort.Sort(sort.Reverse(sort.StringSlice(area_list)))
	for _, v := range area_list{
		data_list = append(data_list, area_map[v])
	}
	return
}
